package com.pactera.library.utils

import android.os.Handler
import android.widget.Toast
import android.os.Looper
import com.pactera.library.config.LibraryConfig


class ToastUtil {

    companion object {
        private var toast: Toast? = null

        private var mHandler = Handler(Looper.getMainLooper())

        public fun showToast(text: String?) {
            showToast(text, Toast.LENGTH_LONG)
        }

        public fun showDebugToast(text: String?) {
            if (LibraryConfig.isDebugMode!!) {
                showToast(text, Toast.LENGTH_LONG)
            }
        }

        private fun showToast(text: String?, duration: Int) {
            if (Looper.myLooper() == Looper.getMainLooper()) {
                show(text, duration)
            } else {
                mHandler.post(Runnable { show(text, duration) })
            }
        }

        private fun show(text: String?) {
            show(text, Toast.LENGTH_LONG)
        }

        private fun show(text: String?, duration: Int) {
            if (toast != null) {
                toast!!.cancel()
            }
            toast = Toast.makeText(LibraryConfig.getApplication(), "app：$text", duration)
            toast!!.show()
        }

    }


}